using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using FinanceStats.Entities;

namespace FinanceStats.Business.Components
{
    
    public delegate void OutcomeItemChangedHandler(UserOutcome oi);

    public partial class OutcomeDayControl : UserControl
    {
        public event OutcomeItemChangedHandler OutcomeItemChanged;
        public event OutcomeItemChangedHandler OnRemoving;

        private OutcomeDay outcomeDay;

        public OutcomeDay OutcomeDay
        {
            get { return outcomeDay; }
            set
            {
                outcomeDay = value;
                ShowDaily();
            }
        }

        public OutcomeDayControl()
        {
            InitializeComponent();
            InitializeDataGrid();
        }

        private void InitializeDataGrid()
        {
            BindingSource bs = new BindingSource();
            bs.DataSource = new List<UserOutcome>();
            
            dataGridView1.DataSource = bs;
            dataGridView1.AutoGenerateColumns = false;

            dataGridView1.Columns["GroupID"].Visible = false;
            dataGridView1.Columns["UserID"].Visible = false;
            dataGridView1.Columns["UserOutcomeID"].Visible = false;
            dataGridView1.Columns["ItemID"].Visible = false;
            dataGridView1.Columns["Date"].Visible = false;

            dataGridView1.Columns["ItemName"].ReadOnly = true;
            dataGridView1.Columns["Amount"].ReadOnly = true;

            dataGridView1.RowHeadersVisible = false;
            this.dataGridView1.AllowUserToAddRows = false;
            this.dataGridView1.AllowUserToOrderColumns = false;
            this.dataGridView1.AllowUserToResizeColumns = false;
            this.dataGridView1.AllowUserToResizeRows = false;
            this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            this.dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dataGridView1.Columns["Amount"].Width = 50;
            dataGridView1.Columns["Amount"].CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
            
            this.dataGridView1.RowHeadersVisible = false;
            this.dataGridView1.AllowUserToDeleteRows = false;
            
            // this.dataGridView1.SelectionChanged += new EventHandler(dataGridView1_SelectionChanged);
            this.dataGridView1.DoubleClick += new EventHandler(dataGridView1_SelectionChanged);
            this.dataGridView1.KeyDown += new KeyEventHandler(dataGridView1_KeyDown);
        }

        public OutcomeDayControl(OutcomeDay od)
        {
            InitializeComponent();
            InitializeDataGrid();
            this.outcomeDay = od;
            ShowDaily();
        }

        void listBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            ListBox lb = sender as ListBox;
            UserOutcome uo = lb.SelectedValue as UserOutcome;
            if (uo != null && this.OutcomeItemChanged != null)
            {
                OutcomeItemChanged(uo);
            }
        }

        private void ShowDaily()
        {
            label1.Text = String.Format("{0}, {1}", outcomeDay.DayName, outcomeDay.Date.ToString("dd/MM/yyyy"));
            BindingSource bs = new BindingSource();
            bs.DataSource = OutcomeDay.OutcomeList;
            label2.Text = String.Format("Total: {0}", outcomeDay.Total);
            dataGridView1.DataSource = bs;
        }

        void dataGridView1_KeyDown(object sender, KeyEventArgs e)
        {
            DataGridView dgw = sender as DataGridView;
            if (e.KeyCode == Keys.Delete && this.OnRemoving != null && dgw.SelectedRows.Count != 0)
            {
                int index = dgw.SelectedRows[0].Index;
                UserOutcome uo = outcomeDay.OutcomeList[index];
                OnRemoving(uo);
            }            
        }

        void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            DataGridView dgw = sender as DataGridView;
            if (dgw.SelectedRows.Count > 0)
            {
                int index = dgw.SelectedRows[0].Index;
                if (this.outcomeDay != null && index <= this.outcomeDay.OutcomeList.Count && this.OutcomeItemChanged != null)
                {
                    UserOutcome uo = outcomeDay.OutcomeList[index];
                    OutcomeItemChanged(uo);
                }
            }
        }
    }
}
